﻿using System;
using System.Net;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Documents;
using System.Windows.Ink;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Animation;
using System.Windows.Shapes;
using SmartSpace.Silverlight;

namespace SmartSpace.Silverlight.Shell.DefaultTheme.Utils
{
    public class NavigationPanellUtil : ISlViewContainerObserver
    {
        private Panel _bar;
        private SlViewContainer _container;
        private Button _ctlBtnBack;

        public NavigationPanellUtil(Button backBtn, SlViewContainer container, Panel bar)
        {
            _ctlBtnBack = backBtn;
            _bar = bar;
            _container = container;
        }

        public void Update(SlViewContainer container)
        {
            _bar.Children.Clear();
            if (container.Views.Count == 1)
            {
                //_ctlBtnBack.Visibility = Visibility.Collapsed;
                Button btn = new Button();
                btn.Tag = container.Views[0];
                btn.Content = container.Views[0].GetCaption();
                btn.Margin = new Thickness(0);
                _bar.Children.Add(btn);
            }
            else
            {
                //_ctlBtnBack.Visibility = Visibility.Visible;

                for (int index = 0; index < container.Views.Count; index++)
                {
                    if (index == container.Views.Count - 1)
                    {
                        Button btn = new Button();
                        btn.Tag = container.Views[index];
                        btn.Content = container.Views[index].GetCaption();
                        btn.Margin = new Thickness(0);
                        btn.Click += new RoutedEventHandler(btn_Click);
                        _bar.Children.Add(btn);
                    }
                    else
                    {
                        Button btn = new Button();
                        btn.Tag = container.Views[index];
                        btn.Content = "../";
                        btn.Margin = new Thickness(0);
                        btn.Click += new RoutedEventHandler(btn_Click);
                        _bar.Children.Add(btn);
                    }
                }
            }
        }

        void btn_Click(object sender, RoutedEventArgs e)
        {
            Button btn = sender as Button;
            SlView view = btn.Tag as SlView;
            _container.GoBack(view);
        }
    }
}
